package com.dev.op.config;

import com.dev.op.VO.ResultVO;
import com.dev.op.enums.ResultCode;
import com.dev.op.exceptions.BizException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class MyAccessDeniedHandler implements AccessDeniedHandler {
    @Override
    public void handle(HttpServletRequest request,
                       HttpServletResponse response,
                       AccessDeniedException accessDeniedException) throws IOException {
        response.setContentType("application/json;charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_FORBIDDEN);

        ResultVO<?> result = ResultVO.error(403, "权限不足，禁止访问");
        response.getWriter().write(new ObjectMapper().writeValueAsString(result));
    }
}


